package com.example.sequences.controller;

import com.example.sequences.domain.Student;
import com.example.sequences.dto.StudentDto;
import com.example.sequences.result.Result;
import com.example.sequences.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * StudentConroller
 *
 * @author maxsc
 * @title StudentConroller.java
 * @date 2018/5/10
 */
@RestController
@RequestMapping("/student")
public class StudentConroller {
    @Autowired
    private StudentService studentService;

    @GetMapping("/all")
    public Result select(){
        Result result = Result.instance ();
        List<StudentDto> list = studentService.queryAll ();
        result.addAttr ( "data", list);
        result.addAttr ( "msg","处理成功" );
        result.addAttr ( "count", list.size ());
        result.success ();
        return result;
    }
    @RequestMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable("id") String id){
        ModelAndView modelAndView = new ModelAndView (  );
        StudentDto one = studentService.findOne ( id );
        modelAndView.addObject ( "student",one );
        modelAndView.setViewName ( "edit" );
        return modelAndView;
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody StudentDto studentDto){
        studentService.up ( studentDto );
        Result result = Result.instance ();
        result.addAttr ( "msg","处理成功" );
        result.success ();
        return result;
    }
}
